package reci.journal.utils;

import java.awt.Desktop;
import java.io.*;
//import java.net.URL;
import java.util.ArrayList;

import reci.journal.Const;

/*
 * Copyright (C) 2014 ReciGames
 * See notice in reci.journal.Main.java.
 */

/**external utilities*/
public class ExtUtil
{
	
//local
	/**
	 * Opens file in system's default program.
	 * 
	 * @param filepath
	 */
	public static void openFile(String filepath)
	{
		boolean success=false;
		
		if(Desktop.isDesktopSupported())
		{
			try
			{
				Desktop.getDesktop().open(new File(filepath));
				success=true;
			}
			catch(IOException e)
			{}
		}
		
		if(!success)
		{
			String os=System.getProperty("os.name").toLowerCase();
			String fp=new File(filepath).getAbsolutePath();
			
			//windows
			if(os.indexOf("win")>=0)
			{
				system("cmd /C start /normal "+filepath);
			}
			//mac
			else if(os.indexOf("mac")>=0)
			{
				system("open "+filepath);
			}
			//*nix
			else if(os.indexOf("nix")>=0 || os.indexOf("nux")>=0)
			{
				system("xdg-open \""+fp+"\"");
			}
		}
	}
	
	/**
	 * Runs system command.
	 * 
	 * @param command
	 */
	public static void system(String command)
	{
		String[] args=command.split(" ");
		ArrayList<String> list=new ArrayList<String>();
		
		for(String arg : args)
		{
			list.add(arg);
		}
		
		system(list);
	}
	
	/**
	 * Runs system command.
	 * 
	 * @param args
	 */
	private static void system(ArrayList<String> args)
	{
		try
		{
			new ProcessBuilder(args).start();
		}
		catch(IOException e)
		{
			Util.alert("Unable to execute command:\n"+e.getMessage());
		}
	}
	
	/**
	 * Copies file 'from' to 'to'.
	 * 
	 * @param from
	 * @param to
	 * @throws IOException
	 */
	public static void copyFile(String from, String to) throws IOException
	{
		BufferedInputStream in=new BufferedInputStream(new FileInputStream(from));
		BufferedOutputStream out=new BufferedOutputStream(new FileOutputStream(to));
		
		copyAndClose(in,out);
	}
	
	/**
	 * Copies 'in' to 'out' and closes streams.
	 * 
	 * @param in
	 * @param out
	 * @throws IOException
	 */
	public static void copyAndClose(InputStream in, OutputStream out) throws IOException
	{
		copyDontClose(in,out);
		
		close(in);
		close(out);
	}
	
	/**
	 * Copies 'in' stream to 'to' file and closes streams.
	 * 
	 * @param in
	 * @param out
	 * @throws IOException
	 */
	public static void copyAndClose(InputStream in, String to) throws IOException
	{
		BufferedOutputStream out=new BufferedOutputStream(new FileOutputStream(to));
		
		copyAndClose(in,out);
	}
	
	/**
	 * Copies 'in' to 'out' but doesn't close streams.
	 * 
	 * @param in
	 * @param out
	 * @throws IOException
	 */
	public static void copyDontClose(InputStream in, OutputStream out) throws IOException
	{
		int numBytesRead;
		byte[] b=new byte[Const.BUFFER];
		
		while((numBytesRead=in.read(b))>=0)
		{
			out.write(b,0,numBytesRead);
		}
	}
	
	/**
	 * Quietly closes.
	 * 
	 * @param c
	 */
	public static void close(Closeable c)
	{
		try
		{
			c.close();
		}
		catch(Exception e)
		{}
	}
	
	/**
	 * Recursively deletes.
	 * 
	 * @param file
	 * @return successful deletion
	 */
	public static boolean deleteFile(File file)
	{
		boolean result=true;
		
		//if directory, delete children
		if(file.isDirectory())
		{
			File[] files=file.listFiles();
			
			for(File f : files)
			{
				result&=deleteFile(f);
			}
		}
		
		//delete argument
		result&=file.delete();
		
		return result;
	}
	
//network
	/**
	 * Opens link.
	 * 
	 * @param url
	 */
	public static void openLink(String url)
	{
		try
		{
			Desktop.getDesktop().browse(java.net.URI.create(url));
		}
		catch(IOException e)
		{
			Util.alert("Unable to open link to "+url+".");
		}
	}
	
//	/**
//	 * Checks for updates.
//	 */
//	public static void checkForUpdates()
//	{
//		try
//		{
//			URL url=new URL("http://recigames.com/s/recijournal/releases/version.f11");
//			InputStream is=url.openStream();
//			int ptr=0;
//			StringBuffer buffer=new StringBuffer();
//			while((ptr=is.read())!=-1)
//			{
//				buffer.append((char)ptr);
//			}
//			
//			String v=buffer.toString();
//			
//			if(!Const.VERSION.equals(v))
//			{
//				Util.info("A new version is available.");
//				openLink("http://recigames.com/s/recijournal/download.php?v="+Const.VERSION);
//			}
//			else
//			{
//				Util.info("This version is up to date.");
//			}
//		}
//		catch(Exception e)
//		{
//			Util.alert("Error contacting the server.");
//		}
//	}
}
